<?php


namespace app\manage\model;


use think\Db;
use think\Exception;

class Goods
{

    /**
     * 干货分享 分类列表
     */
    public function categoryList($page,$page_size){
        $data=Db::name('goods_class')
            ->field('gc_id,gc_name')
            ->where(['is_type'=>2])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('goods_class')
            ->where(['is_type'=>2])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;

    }

    /**
     * 添加分类
     */
    public function categoryAdd($data){
        //验证分类是否重名
        $one=Db::name('goods_class')
            ->where(['gc_name'=>$data['gc_name'],'is_type'=>2])
            ->find();
        if(!empty($one)){
            returnJson(-2004,'该分类名称已存在');
        }
        Db::name('goods_class')->insert($data);
        return true;
    }

    /**
     * 删除 分类
     */
    public function categoryDel($id){
        $goods=Db::name('goods')
            ->where(['gc_id'=>$id])
            ->find();
        if(!empty($goods)){
            returnJson(-2004,'该分类下有商品');
        }
        Db::name('goods_class')
            ->where('gc_id',$id)
            ->delete();
        return true;
    }

    /**
     * 获取所有商品分类
     */
    public function getCateList(){
        $data=Db::name('goods_class')
            ->field('gc_id,gc_name')
            ->where(['is_type'=>2])
            ->select();

        return $data;
    }

    /**
     * 干活分享---商品添加，编辑
     *          热门（裂变）    精选（返利）
     */
    public function goodsAdd($data){

        Db::startTrans();
        try{

            $goods_share=Db::name('system_config')
                ->where(['field_name'=>'goods_share'])
                ->value('field_value');
            $share=json_decode($goods_share,true);

            if(empty($data['goods_id'])){

                //商品名称去重
                $one=Db::name('goods')
                    ->where(['g_name'=>$data['g_name'],'source_type'=>4])
                    ->find();
                if(!empty($one)){
                    returnJson(-2004,'该商品名称已存在');
                }
                $goods_class=Db::name('goods_class')->where('gc_id',$data['gc_id'])->find();
                if($goods_class['gc_name']=='热门视频'){
                    $info['g_type']=2; //裂变
                }else{
                    $info['g_type']=3; //返利
                }
                $info['g_name']=$data['g_name'];
                $info['gc_id']=$data['gc_id'];
                $info['cover_pic']=$data['cover_pic'];
                $info['create_time']=time();
                $info['source_type']=4;

                $spec['name']=$data['spec_name'];
                $spec['num']=$data['spec_num'];
                $spec['price']=$data['price'];
                $spec['cost_price']=$data['cost_price'];
                $spec['coin_price']=$data['coin_price'];

                $test['intro']=$data['intro'];
                $test['video_path']=$data['video_path'];
                $test['product_details_image']=$data['img_list'];
                $test['free_time']=$data['free_time'];



                if($info['g_type']==2){
                    $test['copper_money']= $share['copper_money'];
                    $test['copper_pop']= $share['copper_pop'];
                    $test['silver_money']= $share['silver_money'];
                    $test['silver_pop']= $share['silver_pop'];
                    $test['gold_money']= $share['gold_money'];
                    $test['gold_pop']= $share['gold_pop'];
                }else{
                    $spec['proportion']=$share['proportion'];
                    $spec['gold_coin_num']=$share['gold_coin_num'];
                }

                $goods_id=Db::name('goods')->insertGetId($info);

                $test['goods_id']=$goods_id;
                $spec['goods_id']=$goods_id;

                Db::name('goods_info')->insert($test);

                Db::name('goods_spec')->insert($spec);

            }else{

                //商品名称去重
                $one=Db::name('goods')
                    ->where(['g_name'=>$data['g_name'],'source_type'=>4,'goods_id'=>['neq',$data['goods_id']]])
                    ->find();
                if(!empty($one)){
                    returnJson(-2004,'该商品名称已存在');
                }
                $goods_class=Db::name('goods_class')->where('gc_id',$data['gc_id'])->find();
                if($goods_class['gc_name']=='热门视频'){
                    $info['g_type']=2;
                }else{
                    $info['g_type']=3;
                }

                $goods_id=$data['goods_id'];

                $info['g_name']=$data['g_name'];
                $info['gc_id']=$data['gc_id'];
                $info['cover_pic']=$data['cover_pic'];


                $spec['name']=$data['spec_name'];
                $spec['num']=$data['spec_num'];
                $spec['price']=$data['price'];
                $spec['cost_price']=$data['cost_price'];
                $spec['coin_price']=$data['coin_price'];

                $test['intro']=$data['intro'];
                $test['video_path']=$data['video_path'];
                $test['product_details_image']=$data['img_list'];
                $test['free_time']=$data['free_time'];

                if($info['g_type']==2){
                    $test['copper_money']= empty($data['copper_money']) ? 0 : $data['copper_money'];
                    $test['copper_pop']= empty($data['copper_pop']) ? 0 : $data['copper_pop'];
                    $test['silver_money']= empty($data['silver_money']) ? 0 : $data['silver_money'];
                    $test['silver_pop']= empty($data['silver_pop']) ? 0 : $data['silver_pop'];
                    $test['gold_money']= empty($data['gold_money']) ? 0 : $data['gold_money'];
                    $test['gold_pop']= empty($data['gold_pop']) ? 0 : $data['gold_pop'];

                }

                Db::name("goods")
                    ->where(['goods_id'=>$goods_id])
                    ->update($info);

                Db::name("goods_info")
                    ->where(['goods_id'=>$goods_id])
                    ->update($test);

                Db::name("goods_spec")
                    ->where(['goods_id'=>$goods_id])
                    ->update($spec);
            }


            // 提交事务
            Db::commit();
            return true;

        }catch(Exception $e)
        {
            Db::rollback();
            return false;
        }


    }

    /**
     * 干货分享 ----商品列表  type 1=上架  2=下架
     */
    public function goodsList($filter,$page,$page_size,$type){
        $where=[];
        $where['g.source_type']=4;

        //分类
        if (!empty($filter['gc_id'])) {
            $where['g.gc_id'] = $filter['gc_id'];
        }

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['g.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

       /* $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['g.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['g.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['g.create_time'] = ['<= time', $endTime];
            }
        }*/

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['g.g_name'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['g.status']=1;
        }else{
            $where['g.status']=2;
        }

        $field='g.goods_id,g.g_name,g.cover_pic,g.create_time,g.status,gc.gc_name,gs.price,gs.coin_price';
        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_class gc','gc.gc_id=g.gc_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('goods')->alias('g')
            ->join('goods_class gc','gc.gc_id=g.gc_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 干货分享 ----下架，上架  type 1=上架  2=下架
     */
    public function dismount($type,$goods_id){
        if($type==1){
            Db::name('goods')
                ->where(['goods_id'=>$goods_id])
                ->update(['status'=>1,'update_time'=>time()]);
        }else{
            Db::name('goods')
                ->where(['goods_id'=>$goods_id])
                ->update(['status'=>2,'update_time'=>time()]);
        }

        return true;
    }

    /**
     * 干货分享 ----删除 ，批量删除  type 1=删除单个  2=批量删除
     */
    public function goodsDel($type,$goods_id){
        if($type==1){
            Db::name('goods')
                ->where(['goods_id'=>$goods_id])
                ->update(['is_delete'=>2,'update_time'=>time()]);
        }else{
            Db::name('goods')
                ->where(['goods_id'=>['in',$goods_id]])
                ->update(['is_delete'=>2,'update_time'=>time()]);
        }

        return true;
    }

    /**
     * 干货分享 ----商品详情
     */
    public function goodsDetail($goods_id){
        $field='g.goods_id,g.g_name,g.g_type,g.cover_pic,g.gc_id,gs.price,gs.coin_price,gs.name as spec_name,
        gs.num as spec_num,gs.cost_price,gi.video_path,gi.intro,gi.product_details_image as img_list,
        gi.free_time';
        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->where('g.goods_id',$goods_id)
            ->find();
        if($data['g_type']==2){
            $field2='copper_money,copper_pop,silver_money,silver_pop,gold_money,gold_pop';
            $goods_info=Db::name('goods_info')
                ->field($field2)
                ->where('goods_id',$goods_id)
                ->find();
            $data['copper_money']=$goods_info['copper_money'];
            $data['copper_pop']=$goods_info['copper_pop'];
            $data['silver_money']=$goods_info['silver_money'];
            $data['silver_pop']=$goods_info['silver_pop'];
            $data['gold_money']=$goods_info['gold_money'];
            $data['gold_pop']=$goods_info['gold_pop'];

        }

        $data['video_path'] = json_decode($data['video_path'],true);
        $data['img_list'] = json_decode($data['img_list'],true);

        return $data;
    }


}